/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChatFrame.java
 *
 * Created on 14-nov-2009, 21.07.32
 */
package Messenj;

import Utils.HtmlParser;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JOptionPane;
import net.sf.jml.MsnSwitchboard;
import net.sf.jml.message.MsnDatacastMessage;
import net.sf.jml.message.MsnInstantMessage;

/**
 *
 * @author edoardo
 */
public class ChatFrame extends javax.swing.JFrame {

    MsnSwitchboard sb;
    Vector messageReceived;
    HtmlParser hp;

    /** Creates new form ChatFrame */
    public ChatFrame(MsnSwitchboard ms) {
        initComponents();
        this.sb = ms;
        messageReceived = new Vector();
        hp = new HtmlParser();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jToggleButton3 = new javax.swing.JToggleButton();
        jTextField1 = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 300));
        setResizable(false);

        jEditorPane1.setBackground(java.awt.Color.white);
        jEditorPane1.setContentType("text/html");
        jEditorPane1.setEditable(false);
        jScrollPane1.setViewportView(jEditorPane1);

        jScrollPane2.setViewportView(jList1);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextArea1KeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(jTextArea1);

        jButton1.setText("INVIA");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("TRILLA");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jToggleButton1.setText("GRASSETTO");

        jToggleButton2.setText("CORSIVO");

        jToggleButton3.setText("SOTTOLINEATO");

        jTextField1.setText("1");

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Impostazioni");

        jMenu3.setText("Carattere");
        jMenu2.add(jMenu3);

        jMenu4.setText("Colore");
        jMenu2.add(jMenu4);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 402, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jToggleButton3)
                            .addComponent(jToggleButton2)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                            .addComponent(jToggleButton1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 577, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                        .addComponent(jToggleButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton3)
                        .addGap(6, 6, 6)
                        .addComponent(jToggleButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        MsnInstantMessage mim = new MsnInstantMessage();
        mim.setContent(this.jTextArea1.getText());
        mim.setUnderline(this.jToggleButton3.isSelected());
        mim.setBold(this.jToggleButton1.isSelected());
        mim.setDisplayName(this.sb.getMessenger().getOwner().getEmail().getEmailAddress());
        this.add(mim);
        sb.sendMessage(mim);
        this.jTextArea1.setText("");
    }//GEN-LAST:event_jButton1ActionPerformed

    public void nudgeReceived() {
        int startx = this.getLocationOnScreen().x;
        int starty = this.getLocationOnScreen().y;
        boolean dir = true;
        try {
            new Player("beep3");
        } catch (LineUnavailableException ex) {
            Logger.getLogger(MjChatFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MjChatFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedAudioFileException ex) {
            Logger.getLogger(MjChatFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (int i = 0; i < 50; i++) {
            if (dir) {
                if (i > 0) {
                    startx += 6;
                    starty += 6;
                } else {
                    startx += 3;
                    starty += 3;
                }
                this.setLocation(startx, starty);
            } else {
                startx -= 6;
                starty -= 6;
                this.setLocation(startx, starty);
            }
            dir = !dir;
            try {
                Thread.sleep(60);
            } catch (InterruptedException ex) {
                Logger.getLogger(MjChatFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        this.requestFocus();
    }

    public void add(MsnInstantMessage mim) {
        String s = hp.getParsedMessage(mim);
        messageReceived.add(s);
        String f = "";
        for (int i = 0; i < messageReceived.size(); i++) {
            f = f + this.messageReceived.elementAt(i);
        }
        this.jEditorPane1.setText(f.toString());
    }

    private void jTextArea1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextArea1KeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            MsnInstantMessage mim = new MsnInstantMessage();
            mim.setContent(this.jTextArea1.getText());
            mim.setUnderline(this.jToggleButton3.isSelected());
            mim.setBold(this.jToggleButton1.isSelected());
            this.add(mim);
            sb.sendMessage(mim);
            this.jTextArea1.setText("");
        }
    }//GEN-LAST:event_jTextArea1KeyPressed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        boolean can = true;
        int xtril = 1;
        try {
            xtril = Integer.parseInt(this.jTextField1.getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Messenj - Attenzione", "Il numero di trilli inserito non valido.", JOptionPane.WARNING_MESSAGE);
            can = false;
        }
        if (can) {
            MsnDatacastMessage tril = new MsnDatacastMessage();
            MsnInstantMessage mim = new MsnInstantMessage();
            tril.setId(1);
            for (int i = 0; i < xtril; i++) {
                mim.setContent("Inviati " + (i + 1) + " trilli.");
                this.add(mim);
                try {
                    this.sb.sendMessage(tril);
                    Thread.sleep(200);
                } catch (Exception e) {
                }
            }
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JList jList1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToggleButton jToggleButton3;
    // End of variables declaration//GEN-END:variables
}
